package com.swan.controllers;

import java.util.List;
import java.util.Optional;

import com.swan.beans.ClassRegister;
import com.swan.beans.UserInfo;
import com.swan.models.ClassDao;
import com.swan.utils.CellFactory;

import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.util.Callback;

/**
 * ClassRegistrationController
 * 
 * @author xyh
 * @param <T>
 * @date 20230426
 *
 */

public class ClassRegistrationController {
	@FXML
	private Button DelBtn;

	@FXML
	private TableView<ClassRegister> TableView;
	
	// Table view fields
	@FXML
	private TableColumn<ClassRegister, Boolean> SelectTableColumn;

	@FXML
	private TableColumn<ClassRegister, String> RegTableColumn;
	
	@FXML
	private TableColumn<ClassRegister, String> NameTableColumn;

	@FXML
	private TableColumn<ClassRegister, String> TitleTableColumn;

	@FXML
	private TableColumn<ClassRegister, String> SubjectTableColumn;

	@FXML
	private TableColumn<ClassRegister, String> DescriptionTableColumn;

	@FXML
	private TableColumn<ClassRegister, String> CourseNoTableColumn;

	@FXML
	private TableColumn<ClassRegister, Integer> SectionTableColumn;

	@FXML
	private TableColumn<ClassRegister, Integer> HoursTableColumn;

	@FXML
	private TableColumn<ClassRegister, Integer> CrnTableColumn;

	@FXML
	private TableColumn<ClassRegister, Integer> TermTableColumn;

	@FXML
	private TableColumn<ClassRegister, String> InstructorTableColumn;

	@FXML
	private TableColumn<ClassRegister, Integer> SeatsTableColumn;

	@FXML
	private TableColumn<ClassRegister, String> TimeFrameTableColumn;

	@FXML
	private TableColumn<ClassRegister, String> CampusTableColumn;

	@FXML
	private TableColumn<ClassRegister, String> AttrTableColumn;

	private final ObservableList<ClassRegister> data = FXCollections.observableArrayList();

	private ClassDao dao = new ClassDao();

	@FXML
	public boolean onRegister(ActionEvent event) {
		int size = data.size();
		if (size <= 0) {
			return false;
		}
		for (int i = size - 1; i >= 0; i--) {
			ClassRegister row = data.get(i);
			if (row.getSelected()) {
				Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
				alert.setContentText("confirm  class register ?");
				Optional<ButtonType> showAndWait = alert.showAndWait();
				if (showAndWait.get() == ButtonType.OK) {
					if(row.getReg().equals("NO")) {
						row.setReg("YES");
						data.set(i, row);
						int userId=1;
						if(UserInfo.getLoginUser()!=null) {
							userId=UserInfo.getLoginUser().getUserId();
						}
						dao.registerClass(userId, row.getSeasonClassId());
						System.out.println("add  class register  success!");
					}
				}
			}
		}
		return true;
	}
	

	@FXML
	public boolean onRemoveRegister(ActionEvent event) {
		int size = data.size();
		if (size <= 0) {
			return false;
		}
		for (int i = size - 1; i >= 0; i--) {
			ClassRegister row = data.get(i);
			if (row.getSelected()) {
				Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
				alert.setContentText("confirm remove class register ?");
				Optional<ButtonType> showAndWait = alert.showAndWait();
				if (showAndWait.get() == ButtonType.OK) {
					if(row.getReg().equals("YES")) {
						row.setReg("NO");
						data.set(i, row);
						int userId=1;
						if(UserInfo.getLoginUser()!=null) {
							userId=UserInfo.getLoginUser().getUserId();
						}
						dao.removeRegisterClass(userId,row.getSeasonClassId());
						System.out.println("remove  class register  success!");
					}
				}
			}
		}
		return true;
	}


	@FXML
	private void initialize() {
		// table view
		// set select column ui
		SelectTableColumn
				.setCellFactory(CellFactory.tableCheckBoxColumn(new Callback<Integer, ObservableValue<Boolean>>() {
					@Override
					public ObservableValue<Boolean> call(Integer index) {
						final ClassRegister row = TableView.getItems().get(index);
						ObservableValue<Boolean> ret = new SimpleBooleanProperty(row, "selected", row.getSelected());
						ret.addListener(new ChangeListener<Boolean>() {
							@Override
							public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue,
									Boolean newValue) {
								row.setSelected(newValue);
							}
						});
						return ret;
					}
				}));

		// set column value
		SelectTableColumn.setCellValueFactory(new PropertyValueFactory<ClassRegister, Boolean>("selected"));
		RegTableColumn.setCellValueFactory(new PropertyValueFactory<ClassRegister, String>("reg"));
		NameTableColumn.setCellValueFactory(new PropertyValueFactory<ClassRegister, String>("name"));
		TitleTableColumn.setCellValueFactory(new PropertyValueFactory<ClassRegister, String>("title"));
		SubjectTableColumn.setCellValueFactory(new PropertyValueFactory<ClassRegister, String>("subject"));
		DescriptionTableColumn.setCellValueFactory(new PropertyValueFactory<ClassRegister, String>("description"));
		CourseNoTableColumn.setCellValueFactory(new PropertyValueFactory<ClassRegister, String>("courseNumber"));
		SectionTableColumn.setCellValueFactory(new PropertyValueFactory<ClassRegister, Integer>("section"));
		HoursTableColumn.setCellValueFactory(new PropertyValueFactory<ClassRegister, Integer>("hours"));
		CrnTableColumn.setCellValueFactory(new PropertyValueFactory<ClassRegister, Integer>("crn"));
		TermTableColumn.setCellValueFactory(new PropertyValueFactory<ClassRegister, Integer>("term"));
		InstructorTableColumn.setCellValueFactory(new PropertyValueFactory<ClassRegister, String>("instructor"));
		SeatsTableColumn.setCellValueFactory(new PropertyValueFactory<ClassRegister, Integer>("seats"));
		TimeFrameTableColumn.setCellValueFactory(new PropertyValueFactory<ClassRegister, String>("timeFrame"));
		CampusTableColumn.setCellValueFactory(new PropertyValueFactory<ClassRegister, String>("campus"));
		AttrTableColumn.setCellValueFactory(new PropertyValueFactory<ClassRegister, String>("attribute"));

		// set table view
		int userId=1;
		if(UserInfo.getLoginUser()!=null) {
			userId=UserInfo.getLoginUser().getUserId();
		}
		System.out.println("userId is"+userId);
		List<ClassRegister> list = dao.listClassRegister(userId);
		System.out.println("list class register success!");
		data.addAll(list);

		TableView.setItems(data);
		TableView.setEditable(true);

	}

}
